// 封装调用api 获取传参等方法的接口
(function (){
    window.b5config = {
        display:0,
        domain : "http://front.zichudongfang.com/school_youer/zlhx", //接口地址
    }
})();
//统一请求方法
function request(config) {
    config = config || {};
    var successFunc = config.success || function () {};
    var failFunc = config.fail || successFunc;
    var defaultConfig = {
        method: config.type,
        dataType: "json",
        contentType: 'application/x-www-form-urlencoded',
        // contentType: 'application/json;charset=UTF-8',
        data:{}
    }
    //参数合并
    var data = {}
    data = Object.assign(data, config.hasOwnProperty('data')?config.data:{});
    defaultConfig = Object.assign(defaultConfig, config);

    defaultConfig.data = data;

    if(defaultConfig.contentType === 'application/json'){
        defaultConfig.data = JSON.stringify(data);
    }
    //请求接口拼接统一域名
    var url = config.url ? config.url : "";
    url = window.b5config.domain + url;
    defaultConfig.url = url;


    defaultConfig.success = function (result) {
        if (result.err_code === 0) {
            var data = result.hasOwnProperty('data')?result.data:null;
            successFunc({code:1,msg:'',data:data});
        }else{
            successFunc({code:0,msg:result.err_msg,data:[]});
        }
    }
    defaultConfig.fail = function () {
        failFunc({code:0,msg:'网络请求失败',data:[]});
    }
    $.ajax(defaultConfig);
}
// 封装api调用示例
// request({url:'/school/figure/list',data:{do:'getimage'},type:'GET',success:(res)=>{
// 	console.log(res)
// }})



//获取url中的参数
function GetRequest(params) {
    var url = window.location.search;
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
        }
    }
    if (params) {
        if (theRequest.hasOwnProperty(params)) {
            return theRequest[params];
        } else {
            return "";
        }
    }
    return theRequest;
}
// 使用示例
// let name = GetRequest('name')
